package com.cjbdi.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.cjbdi.entity.Report;

@Repository
public interface ReportRepository extends JpaRepository<Report, Long> {
	
	@Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "delete from report where id=:id",nativeQuery = true)//原生SQL
    Integer delete(@Param("id")int id);
    
    @Query(value = "select * from report where case when :title!=''then title like '%'||:title||'%' else 1=1 end and case when :fymc!='' then fymc like '%'||:fymc||'%' else 1=1 end and case when :type!='' then type=:type else 1=1 end" , nativeQuery = true)
    Page<Report> getPageByParam(String title,String fymc,String type,Pageable pageable);
    
    /*@Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update report set count=count+1 where id=:id",nativeQuery = true)//原生SQL
    Integer updateCount(@Param("id")String id);*/
    
    @Query(value = "select * from Report as a where a.title = :title", nativeQuery = true)
    List<Report> findByTitle(@Param("title")String title);
    
    @Query(value = "select * from report where id = :id", nativeQuery = true)
    List<Report> findById(@Param("id")int id);
    
    @Query(value = "select * from report where fybm = :fybm", nativeQuery = true)
    List<Report> findByFybm(@Param("fybm")String fybm);
    
    @Query(value = "select * from report where fybm = :fybm and year=:year and type=:type and zs_type=:zsType and time_dimension=:timeDimension", nativeQuery = true)
    List<Report> findByParam(@Param("fybm")String fybm,@Param("year")String year,@Param("type")String type,@Param("zsType")String zsType,@Param("timeDimension")String timeDimension);
    
}
